<?php

function image_data($album_id, $Eimage_id){
  $album_id = (int)$album_id;
  $Eimage_id = (int)$Eimage_id;
  //print_r $album_id;
  $args = func_get_args();
  unset($args[0], $args[1]);
  $fields = '`'.implode('`, `', $args).'`';
  
  $query = mysql_query("SELECT $fields FROM `imagesVU` WHERE `image_id`=$Eimage_id AND `user_id`=".$_SESSION['user_id']);
  $query_result = mysql_fetch_assoc($query);
  foreach ($args as $field) {
    $args[$field] = $query_result[$field];
  } 
	return $args;
}

function upload_image($image_temp, $image_ext, $album_id, $image_keywords) {
	$album_id = (int)$album_id;
	$image_keywords = mysql_real_escape_string(htmlentities($image_keywords));
	
	mysql_query("INSERT INTO `imagesVU` VALUES ('', '".$_SESSION['user_id']."', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_keywords')");
	
	$image_id = mysql_insert_id();
	$image_file = $image_id.'.'.$image_ext;
	move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);
	//chmod ($image_temp, 0755);
	chmod ('uploads/'.$album_id.'/'.$image_file, 0755);
	create_thumb('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');
	chmod ('uploads/thumbs/'.$album_id.'/'.$image_file, 0755);
}

function get_images($album_id) {
  $album_id = (int)$album_id;
  
  $images = array();
  
  $image_query = mysql_query("SELECT `image_id`, `album_id`, `timestamp`, `ext`, `image_keywords` FROM `imagesVU` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
  while ($images_row = mysql_fetch_assoc($image_query)) {
	$images[] = array(
				'id' => $images_row['image_id'],
				'album' => $images_row['album_id'],
				'timestamp' => $images_row['timestamp'],
				'ext' => $images_row['ext'],
				'keywords' => $images_row['image_keywords']
	);
  }
  return $images;
}

function image_check($image_id) {
  $image_id = (int)$image_id;
  $query = mysql_query("SELECT COUNT(`image_id`) FROM `imagesVU` WHERE `image_id`=$image_id AND `user_id`=".$_SESSION['user_id']);
  return (mysql_result($query, 0) == 0) ? false : true;
}

function edit_image($album_id, $image_id, $image_keys){
  $album_id = (int)$album_id;
  $image_id = (int)$image_id;
  $image_keys = mysql_real_escape_string($image_keys);
  
  mysql_query("UPDATE `imagesVU` SET `image_keywords`='$image_keys' WHERE `album_id`=$album_id AND `image_id`=$image_id AND `user_id`=".$_SESSION['user_id']);
}

function delete_image($image_id) {
  $image_id = (int)$image_id;
  
  $image_query = mysql_query("SELECT `album_id`, `ext` FROM `imagesVU` WHERE `image_id`= $image_id AND `user_id`=".$_SESSION['user_id']);
  $image_result = mysql_fetch_assoc($image_query);
  
  $album_id = $image_result['album_id'];
  $album_ext = $image_result['ext'];
  
  unlink('uploads/'.$album_id.'/'.$image_id.'.'.$album_ext);
  //print_r('uploads/'.$album_id.'/'.$image_id.'.'.$album_ext);
  unlink('uploads/thumbs/'.$album_id.'/'.$image_id.'.'.$album_ext);

  mysql_query("DELETE FROM `imagesVU` WHERE `image_id`=$image_id AND `user_id`=".$_SESSION['user_id']);
}


?>
